package cn.chenxing.module.query.controller;

import cn.chenxing.domain.dto.TrackData;
import cn.chenxing.domain.dto.TrackLine;
import cn.chenxing.domain.dto.TrackPoint;
import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.threadlocal.ThreadData;
import cn.chenxing.domain.util.DateUtil;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.utils.TableUtil;
import com.jfoenix.controls.JFXComboBox;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 距离查询的控制层
 * @Author maogen.ymg
 * @Date 2020/4/14 17:01
 */
@Lazy
@FXMLController
@Slf4j
public class QueryDistController implements Initializable {

    @FXML
    private TextField distLeft;

    @FXML
    private TextField distRight;

    @FXML
    private JFXComboBox<String> distUnit;

    @FXML
    private BorderPane borderPane;

    @FXML
    private Label tipLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        distUnit.getItems().addAll("公里", "米");
        distUnit.setValue("公里");
    }

    @FXML
    public void query() {
        if(ThreadData.get().getTrackData() == null) {
            DialogBuilderUtil.showPositiveAlert(distLeft, "无法进行查询", "还没导入数据，请选择文件菜单进行导入");
            return;
        }

        String distLeftStr = distLeft.getText().trim();
        String distRightStr = distRight.getText().trim();

        boolean hasLeft = true;
        boolean hasRight = true;
        if(StringUtils.isBlank(distLeftStr)) {
            hasLeft = false;
        }
        if(StringUtils.isBlank(distRightStr)) {
            hasRight = false;
        }
        // 左边大于右边 -- 错误
        DialogBuilderUtil.showPositiveAlert(hasLeft && hasRight &&
                Double.parseDouble(distLeftStr) > Double.parseDouble(distRightStr), distUnit,"提示", "距离范围填写有误");

        // 显示在表格上
        TableUtil.importTableView(borderPane,
                getTableData(distLeftStr, distRightStr, hasLeft, hasRight), false);

    }

    @FXML
    public void clear() {
        distLeft.setText("");
        distRight.setText("");
        borderPane.getChildren().clear();
        borderPane.setCenter(tipLabel);
    }

    /**
     * 获取表格数据
     * @return List<TrackTable>
     */
    private List<TrackTable> getTableData(String distLeftStr, String distRightStr, boolean hasLeft, boolean hasRight) {
        List<TrackTable> trackTableList = new ArrayList<>();
        // 用户数据
        List<TrackLine> trackLineList = ThreadData.get().getTrackData().getTrackLineList();
        for(TrackLine trackLine : trackLineList) {
            // 全部
            boolean isNeedData = true;
            // 数据
            double distance = "公里".equals(distUnit.getValue()) ? trackLine.getDistance()/1000 : trackLine.getDistance();
            // 都有限制
            if(hasLeft && hasRight) {
                isNeedData = distance > Double.parseDouble(distLeftStr)
                        && distance < Double.parseDouble(distRightStr);
            }else if(hasLeft){
                // 左边限制
                isNeedData = distance > Double.parseDouble(distLeftStr);
            }else if(hasRight){
                // 右边限制
                isNeedData = distance < Double.parseDouble(distRightStr);
            }

            if(isNeedData) {
                for(TrackPoint trackPoint : trackLine.getTrackPointList()) {
                    TrackTable trackTable = new TrackTable();
                    trackTable.setId(trackLine.getId())
                            .setLongitude(trackPoint.getLongitude())
                            .setLatitude(trackPoint.getLatitude())
                            .setTime(DateUtil.timeStamp2Date(trackPoint.getTimeStamp(), null))
                            .setSpeed(trackPoint.getSpeed());
                    trackTableList.add(trackTable);
                }
            }

        }

        // 保存为公有数据
        TrackData trackData = ThreadData.get().getTrackData();
        trackData.setTrackLineList(trackLineList);
        ThreadData.get().setTrackData(trackData);

        return trackTableList;
    }

}
